import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scripting.support.ResourceScriptSource;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Luademo {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    private DefaultRedisScript<String> redisScript = new DefaultRedisScript<>();

    /**
     * 判断是否购买成功的方法
     *
     * @param id   商品id
     * @param size 购买数量
     */
    public void testlus(int id, int size) {

        /*根据id进行查询*/

        /**
         * product product = mapper.selectByid(id);
         * springRedisTemplate.opsForValue().set("set","seckill:stock:"+id,product.getSize());
         */

        /* 指定录取脚本路径                 资源脚本路径               加载指定资源文件 */
        redisScript.setScriptSource(new ResourceScriptSource(new ClassPathResource(File.separator + "redis" + File.separator + "test.lua")));
        redisScript.setResultType(String.class);        //返回值是String 类型
        List<String> list = new ArrayList<>();          //定义key的集合
        list.add("seckill:stock:" + id);                  //向key集合当中增加传递的key值
        String str = stringRedisTemplate.execute(redisScript, list, "" + size);
        System.out.println(str);

        /**
         *         根据返回值对数据库进行操作
         */


    }
}
